package com.logistics.version.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.route.entity.domain.Route;
import com.logistics.utils.aspect.annotation.Insert;
import com.logistics.utils.aspect.annotation.Pager;
import com.logistics.utils.bean.ResultPager;
import com.logistics.utils.configure.Configure;
import com.logistics.utils.constants.Message;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.message.Manager;
import com.logistics.utils.tool.StringUtils;
import com.logistics.version.entity.domain.Version;
import com.logistics.version.entity.model.VersionPager;
import com.logistics.version.entity.model.VersionRow;
import com.logistics.version.entity.model.VersionSave;
import com.logistics.version.mapper.VersionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Collectors;

@Service
public class VersionService extends ServiceImpl<VersionMapper, Version> {


    @Autowired
    private Configure configure;

    /**
     * 查找版本列表
     * @param param
     * @return
     */
    @Pager
    public ResultPager pager(VersionPager param){
        IPage page = new Page(param.getCurrent(), param.getPageSize());
        QueryWrapper<Version> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.isNotNull(param.getAppName()),
                Version::getAppName, param.getAppName())
                .eq(StringUtils.isNotNull(param.getVersionName()),
                        Version::getVersionName, param.getVersionName())
                .orderByDesc(Version::getVersionCode);
        IPage<Version> result = this.page(page, queryWrapper);
        return new ResultPager(result.getRecords().stream().
                map(VersionRow::build).collect(Collectors.toList()), (int)result.getTotal());
    }

    /**
     * 查找最大版本
     * @return
     */
    public VersionRow maxVersion(){
        return VersionRow.build(this.getOne(new LambdaQueryWrapper<Version>().orderByDesc(Version::getVersionCode).last("limit 1")));
    }

    /**
     * 查询详细
     * @param id
     * @return
     */
    public VersionRow detail(String id){
        return VersionRow.build(this.getById(id));
    }

    /**
     * 保存
     * @param save
     */
    @Insert
    @Transactional
    public void save(VersionSave save){
        if(StringUtils.isNull(save.getId())){
            Version domain = Version.build(save);
            domain.setVersionPath(configure.getMinioUrl());
            this.save(domain);
        }else{
            this.updateById(Version.build(save));
        }
    }

    /**
     * 删除
     * @param ids
     */
    public void remove(String ids) throws MessageException {
        if(StringUtils.isNull(ids)){
            throw new MessageException(Manager.getMessage(Message.MSG1005.getCode()));
        }
        this.remove(new LambdaQueryWrapper<Version>().in(Version::getId, StringUtils.splitToList(ids, ",")));
    }
}
